home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3111 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.5 KB

  1. Path: zambak.bcc.bilkent.edu.tr!hitit!uysal
  2. From: uysal@bilkent.edu.tr (Ertugrul Uysal)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Friends....
  5. Date: 22 Jan 1996 08:47:47 GMT
  6. Organization: Bilkent University
  7. Message-ID: <4dvivj$ile@zambak.bcc.bilkent.edu.tr>
  8. References: <4c9srh$g2l@ixnews3.ix.netcom.com>
  9. NNTP-Posting-Host: hitit.bcc.bilkent.edu.tr
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Chris Morris (cpmorris@popd.ix.netcom.com) wrote:
  13. :     I am a beginner with C++ and have a question concerning the
  14. : declaration of 'friend' functions.  I am working with Windows
  15. : programming, but this question doesn't apply to Windows, it is a C++
  16. : question.
  17.  
  18. :     This is the class code I am confused about
  19.  
  20.  
  21. : class TLine : public TPoints {
  22. :   public:
  23. :     TLine(int penSize = 1) : TPoints(10, 0, 10)
  24. :     {
  25. :       PenSize = penSize;
  26. :     }
  27.  
  28. :     int QueryPen() const
  29. :     {
  30. :       return PenSize;
  31. :     }
  32.  
  33. :     int QueryPen(int penSize);
  34.  
  35. :     // The == operator must be defined for the container class, even
  36. :     if unused
  37. :     bool operator ==(const TLine& other) const
  38. :     {
  39. :       return &other == this;
  40. :     }
  41.  
  42. :     friend ostream& operator <<(ostream& os, const TLine& line);
  43. :     friend istream& operator >>(istream& is, TLine& line);
  44.  
  45. :   protected:
  46. :     int PenSize;
  47. : };
  48.  
  49.  
  50. :     Why are the two operator functions (<< and >>) declared as friends?
  51. : Is it because the original declarations are not virtual and cannot be
  52. : overridden?  I understand that these function have to be modified
  53. : because they deal with a user-defined type.  But look at the
  54. : definitions of the functions
  55.  
  56. These are operator functions not member functions so they can not be virtual! 
  57. : ostream&
  58. : operator <<(ostream& os, const TLine& line)
  59. : {
  60. :   // Write the number of points in the line
  61. :   os << line.GetItemsInContainer();
  62.  
  63. :   // Write the pen size
  64. :   os << ' ' << line.PenSize;
  65.  
  66. :   // Get an iterator for the array of points
  67. :   TPointsIterator j(line);
  68.  
  69. :   // While the iterator is valid (i.e. we haven't run out of points)
  70. :   while(j)
  71. :     // Write the point from the iterator and increment the array.
  72. :     os << j++;
  73.  
  74. :   os << '\n';
  75. :   // return the stream object
  76. :   return os;
  77. : }
  78.  
  79. : istream&
  80. : operator >>(istream& is, TLine& line)
  81. : {
  82. :   unsigned numPoints;
  83.  
  84. :   is >> numPoints;
  85.  // this one operates on numPoints which is unsigned so it uses the original >> definition
  86. :   is >> line.PenSize;
  87.  
  88. :   while (numPoints--) {
  89. :     TPoint point;
  90. :     is >> point;
  91. // operator >> should have been overloaded  for TPoint class also
  92. :     line.Add(point);
  93. :   }
  94. :   // return the stream object
  95. :   return is;
  96. : }
  97.  
  98.  
  99. :     This is the confusing part.  How can the << and >> be used in the
  100. : definitions of << and >>?  Is seems like the new << and >> friend
  101. : functions are just defining their own implementations for handling the
  102. : user-defined type.  This is overloading, isn't it?
  103. the ones that are used inside the functions operate on ordinary data types i.e. unsigned so they use the original definitions of << and >>, the ones declared before. As you said this is overloading , but it is operator overloading. Operator functions are not members of any class, so they can not be declared virtual. 
  104.  
  105. :     I have, and am nearly finished with "The C++ Programming Language" by
  106. : Bjarne Stroustrup.  When I look at the friend function section he sayS
  107. : that friends are used so that two classes can have functions in
  108. : common.  The function is able to access the private part of each
  109. : class.  But he says nothing about using a 'friend' declaration to do
  110. : what this example is doing.  Help?
  111.  Well this example is used so that the << and >> operator functions can access the private parts of the Tline class and the istream class. So he says something about what this example is doing using the friend declaration. 
  112. :     Thanks in advance...
  113.  
  114.  
  115. : -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  116.  
  117. :                Chris Morris  (cpmorris@ix.netcom.com)
  118. :                P. O. Box 574                       
  119. :                Clearwater, FL  34617-0574
  120.  
  121.  
  122. : Reality is a crutch for people who can't deal with science fiction . . .
  123. : -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  124. Learning C++ is a reality. So there are realities which even people dealing with Science fiction can not deal with. Another example is the reality of this poor guy, who has a 1.00 less CGPA then it could have been, as he likes reading Arthur C. Clarke especially during finals (the time period when I want to go to another galaxy)
  125. Ertugrul Uysal
  126. uysal@cs.bilkent.edu.tr
  127.